#ifndef AER_RENDERTARGET_HPP_
#define AER_RENDERTARGET_HPP_

#include "aerDeviceResource.hpp"


namespace aer {

/**
 *  \class RenderTarget
 * 
 *  Abstract class used to render into a buffer.
 *  Currently, only for writing (target GL_FRAMEBUFFER).
 * 
 */
class RenderTarget : public DeviceResource
{    
  protected:
    U32 m_width;
    U32 m_height;
    bool m_bResized;//
  
  
  public:
    static bool CheckStatus() {
      return GL_FRAMEBUFFER_COMPLETE == glCheckFramebufferStatus(GL_FRAMEBUFFER);
    }
    
    
  public:
    RenderTarget() 
        : DeviceResource(), 
          m_width(0), 
          m_height(0),
          m_bResized(false)
    {}
    
        
    void generate()
    {
      AER_ASSERT( !isGenerated() );
      glGenFramebuffers( 1, &m_id);
    }
    
    void release()
    {
      if (isGenerated()) 
      {
        glDeleteFramebuffers( 1, &m_id);
        m_id = 0u;
      }
    }
    
    void bind()
    {
      AER_ASSERT( isGenerated() );
      glBindFramebuffer( GL_FRAMEBUFFER, m_id);
    }
    
    static
    void unbind()
    {
      glBindFramebuffer( GL_FRAMEBUFFER, 0u);
    }
    
    virtual void begin() = 0;
    virtual void end() { unbind(); }  

    // =============
    // == Setters ==
    // =============
    virtual void resize(const U32 width, const U32 height)
    {
      m_width  = width;
      m_height = height;
      m_bResized = true;
    }    
    
    // =============
    // == Getters ==
    // =============
    unsigned int getWidth() const { return m_width; }
    unsigned int getHeight() const { return m_height; } 

};
  
} // aer

#endif // AER_RENDERTARGET_HPP_
